import Cookies from 'js-cookie'

const TOKEN_KEY = 'convallaria_token'
const REFRESH_TOKEN_KEY = 'convallaria_refresh_token'

// 获取token
export const getToken = (): string => {
  return Cookies.get(TOKEN_KEY) || ''
}

// 设置token
export const setToken = (token: string, rememberMe: boolean = false): void => {
  const expires = rememberMe ? 30 : 7 // 记住我30天，否则7天
  Cookies.set(TOKEN_KEY, token, { expires })
}

// 移除token
export const removeToken = (): void => {
  Cookies.remove(TOKEN_KEY)
  Cookies.remove(REFRESH_TOKEN_KEY)
}

// 获取刷新token
export const getRefreshToken = (): string => {
  return Cookies.get(REFRESH_TOKEN_KEY) || ''
}

// 设置刷新token
export const setRefreshToken = (token: string): void => {
  Cookies.set(REFRESH_TOKEN_KEY, token, { expires: 30 }) // 30天过期
}

// 检查是否已登录
export const isLoggedIn = (): boolean => {
  return !!getToken()
}
